﻿using System;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace iData
{
    public class mysqlDataAcess
    {
        public Setting dbSetting = new Setting();
        public mysqlDataAcess()
        {
            dbSetting.server = System.Web.Configuration.WebConfigurationManager.AppSettings["server"];
            dbSetting.database = System.Web.Configuration.WebConfigurationManager.AppSettings["database"];
            dbSetting.uid = System.Web.Configuration.WebConfigurationManager.AppSettings["uid"];
            dbSetting.pwd = System.Web.Configuration.WebConfigurationManager.AppSettings["pwd"];

            connectionString = string.Format("Database='{1}';Data Source='{0}';User Id='{2}';Password='{3}';charset='utf8';pooling=true",
               dbSetting.server, dbSetting.database, dbSetting.uid, dbSetting.pwd);
        }

        public mysqlDataAcess(Setting db)
        {
            dbSetting = db;
            dbSetting.server = System.Web.Configuration.WebConfigurationManager.AppSettings["server"];
            dbSetting.database = System.Web.Configuration.WebConfigurationManager.AppSettings["database"];
            dbSetting.uid = System.Web.Configuration.WebConfigurationManager.AppSettings["uid"];
            dbSetting.pwd = System.Web.Configuration.WebConfigurationManager.AppSettings["pwd"];

            connectionString = string.Format("Database='{1}';Data Source='{0}';User Id='{2}';Password='{3}';charset='utf8';pooling=true",
                dbSetting.server, dbSetting.database, dbSetting.uid, dbSetting.pwd);
        }

        #region 以sqlparam为参数的方法
        public DataSet Execute(string strSql, List<SqlParameter> paraList=null)
        {
            return GetDataSet(strSql, ToParaArray(paraList));
        }
        public DataRow ExecuteOneRow(string strSql, List<SqlParameter> paraList=null)
        {
            DataSet dset = Execute(strSql, paraList);
            DataRow dr = null;
            if (dset.Tables[0].Rows.Count > 0) dr = dset.Tables[0].Rows[0];
            return dr;
        }
        public string ExecuteScalar(string strSql, List<SqlParameter> paraList)
        {
            return ExecuteScalar(strSql, ToParaArray(paraList)).ToString();
        }
        public int ExecuteCount(string strSql, List<SqlParameter> paraList=null)
        {
            var o = ExecuteScalar(strSql, paraList);
            int n = 0;
            try
            {
                n = Convert.ToInt32(o);
            }
            catch { }
            return n;
        }
        public int ExecuteInsert(string strSql, List<SqlParameter> paraList)
        {
            //strSql += ";select @@IDENTITY";
            return ExecuteCount(strSql, paraList == null ? null : paraList);
        }
        public int ExecuteNonQuery(string strSql, List<SqlParameter> paraList)
        {
            return ExecuteNonQuery(strSql, ToParaArray(paraList));
        }
        public int ExecuteNonQuery(string strSql)
        {
            MySqlParameter[] commandParameters=null;
            return ExecuteNonQuery(strSql,commandParameters );
        }
        public string ExecuteScalar(string strSql)
        {
            MySqlParameter[] commandParameters = null;
            return ExecuteScalar(strSql, commandParameters).ToString();
        }
 
        private List<MySqlParameter> ToMysqlParmList(List<SqlParameter> paraList)
        {
            List<MySqlParameter> list = new List<MySqlParameter>();
            if (paraList == null) return null;
            foreach (var e in paraList)
            {
                list.Add(new MySqlParameter(e.ParameterName, e.Value));
            }
            return list;
        }
        private MySqlParameter[] ToParaArray(List<SqlParameter> paraList)
        {
            if (paraList == null) return null;
            return ToMysqlParmList(paraList).ToArray();
        }
        
        #endregion
        //数据库连接字符串
        //public static string Conn = "Database='数据库名';Data Source='数据库服务器地址';User Id='数据库用户名';Password='密码';charset='utf8';pooling=true";
        public string connectionString = "";
        public CommandType cmdType = CommandType.Text;
        // 用于缓存参数的HASH表
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        #region 原有代码

        /// <summary>
        ///  给定连接的数据库用假设参数执行一个sql命令（不返回数据集）
        /// </summary>
        /// <param name="connectionString">一个有效的连接字符串</param>
        /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
        /// <param name="cmdText">存储过程名称或者sql命令语句</param>
        /// <param name="commandParameters">执行命令所用参数的集合</param>
        /// <returns>执行命令所影响的行数</returns>
        private int ExecuteNonQuery(string cmdText, params MySqlParameter[] commandParameters)
        {
            MySqlCommand cmd = new MySqlCommand();
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }


        /// <summary>
        ///使用现有的SQL事务执行一个sql命令（不返回数据集）
        /// </summary>
        /// <remarks>
        ///举例:
        ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
        /// </remarks>
        /// <param name="trans">一个现有的事务</param>
        /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
        /// <param name="cmdText">存储过程名称或者sql命令语句</param>
        /// <param name="commandParameters">执行命令所用参数的集合</param>
        /// <returns>执行命令所影响的行数</returns>
        public int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
        {
            MySqlCommand cmd = new MySqlCommand();
            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }

        /// <summary>
        /// 用执行的数据库连接执行一个返回数据集的sql命令
        /// </summary>
        /// <remarks>
        /// 举例:
        ///  MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
        /// </remarks>
        /// <param name="connectionString">一个有效的连接字符串</param>
        /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
        /// <param name="cmdText">存储过程名称或者sql命令语句</param>
        /// <param name="commandParameters">执行命令所用参数的集合</param>
        /// <returns>包含结果的读取器</returns>
        public MySqlDataReader ExecuteReader(string cmdText, params MySqlParameter[] commandParameters)
        {
            //创建一个MySqlCommand对象
            MySqlCommand cmd = new MySqlCommand();
            //创建一个MySqlConnection对象
            MySqlConnection conn = new MySqlConnection(connectionString);

            //在这里我们用一个try/catch结构执行sql文本命令/存储过程，因为如果这个方法产生一个异常我们要关闭连接，因为没有读取器存在，
            //因此commandBehaviour.CloseConnection 就不会执行
            try
            {
                //调用 PrepareCommand 方法，对 MySqlCommand 对象设置参数
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                //调用 MySqlCommand  的 ExecuteReader 方法
                MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                //清除参数
                cmd.Parameters.Clear();
                return reader;
            }
            catch
            {
                //关闭连接，抛出异常
                conn.Close();
                throw;
            }
        }

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="connectionString">一个有效的连接字符串</param>
        /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
        /// <param name="cmdText">存储过程名称或者sql命令语句</param>
        /// <param name="commandParameters">执行命令所用参数的集合</param>
        /// <returns></returns>
        private DataSet GetDataSet(string cmdText, params MySqlParameter[] commandParameters)
        {
            //创建一个MySqlCommand对象
            MySqlCommand cmd = new MySqlCommand();
            //创建一个MySqlConnection对象
            MySqlConnection conn = new MySqlConnection(connectionString);

            //在这里我们用一个try/catch结构执行sql文本命令/存储过程，因为如果这个方法产生一个异常我们要关闭连接，因为没有读取器存在，

            try
            {
                //调用 PrepareCommand 方法，对 MySqlCommand 对象设置参数
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                //调用 MySqlCommand  的 ExecuteReader 方法
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet ds = new DataSet();

                adapter.Fill(ds);
                //清除参数
                cmd.Parameters.Clear();
                conn.Close();
                return ds;
            }
            catch (Exception e)
            {
                IO.WriteExecuteLog("error in ExecuteScalar:\n\r" + cmdText + "\n\r" + e.Message);
                throw e;
            }
        }

        public DataSet GetDataSet(string cmdText)
        {
            return GetDataSet(cmdText, null);
        }

        /// <summary>
        /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
        /// </summary>
        /// <remarks>
        ///例如:
        ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
        /// </remarks>
        ///<param name="connectionString">一个有效的连接字符串</param>
        /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
        /// <param name="cmdText">存储过程名称或者sql命令语句</param>
        /// <param name="commandParameters">执行命令所用参数的集合</param>
        /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
        private object ExecuteScalar(string cmdText, params MySqlParameter[] commandParameters)
        {
            MySqlCommand cmd = new MySqlCommand();

            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
                object val = cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                if (val == null) val = "";
                return val;
            }
        }


        /// <summary>
        /// 将参数集合添加到缓存
        /// </summary>
        /// <param name="cacheKey">添加到缓存的变量</param>
        /// <param name="commandParameters">一个将要添加到缓存的sql参数集合</param>
        public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters)
        {
            parmCache[cacheKey] = commandParameters;
        }

        /// <summary>
        /// 找回缓存参数集合
        /// </summary>
        /// <param name="cacheKey">用于找回参数的关键字</param>
        /// <returns>缓存的参数集合</returns>
        public static MySqlParameter[] GetCachedParameters(string cacheKey)
        {
            MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey];

            if (cachedParms == null)
                return null;

            MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length];

            for (int i = 0, j = cachedParms.Length; i < j; i++)
                clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone();

            return clonedParms;
        }

        /// <summary>
        /// 准备执行一个命令
        /// </summary>
        /// <param name="cmd">sql命令</param>
        /// <param name="conn">OleDb连接</param>
        /// <param name="trans">OleDb事务</param>
        /// <param name="cmdType">命令类型例如 存储过程或者文本</param>
        /// <param name="cmdText">命令文本,例如:Select * from Products</param>
        /// <param name="cmdParms">执行命令的参数</param>
        private void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
        {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null)
            {
                foreach (MySqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
        
        #endregion

           public class Setting
    {
        public string server { get; set; }
        public string database { get; set; }
        public string uid { get; set; }
        public string pwd { get; set; }

    }
    }
    public class Setting
    {
        public string server { get; set; }
        public string database { get; set; }
        public string uid { get; set; }
        public string pwd { get; set; }

    }
}